#include "kcpagent.h"
#include "easylogging++.h"

void KCPAgent::ReceiveData(shared_ptr<std::vector<unsigned char> > data){
	kcp->Input(data);
}

void KCPAgent::Update(double delta) {
	kcp->Update(delta);
	while(true) {
		auto pack = kcp->Recv();
		
		if(pack != nullptr) {
			pack->remoteEnd = remoteEnd;
			kcpServer->ReceiveKCPPacket(pack);
		}else {
			break;
		}
	}
	LOG(INFO) << "FinishKcpAgent";
}

//KCP-->UDP
void KCPAgent::SendPacket(shared_ptr<KCPPacket> pa) {
	if(!IsClose) {
		pa->remoteEnd = remoteEnd;
		kcpServer->SendUDPPacket(pa);
	}
}

//KCP层超时关闭
void KCPAgent::KCPClosed() {
	IsClose = true;
	kcpServer->RemoveAgent(this);
}

void KCPAgent::SendData(shared_ptr<std::vector<unsigned char> > data) {
	kcp->Send(data);
}

void KCPAgent::Close() {
	IsClose = true;
}